<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button class="btn">按钮1</button>
  <button class="btn">按钮2</button>
  <button class="btn">按钮3</button>
  <button class="btn">按钮4</button>
  
  <script>
    var btnEl = document.querySelector(".btn")
    // btnEls.onclick = function() {
    //   console.log("点击了按钮");
    // }
    
    var btnEls = document.querySelectorAll(".btn")
    for (let i = 0; i < btnEls.length; i++) {
      var btn = btnEls[i];
      btn.onclick = function() {
        console.log(`按钮${i+1}被点击了`)
      }
    }
  </script>
</body>
</html>